package com.zhentao.com.zhentao;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhentao.pojo.TbStudent;
import com.zhentao.service.*;
import com.zhentao.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/student")
public class StudentController {
    @Autowired
    TbStudentService tbStudentService;
    @Autowired
    TbTeacherService tbTeacherService;
    @Autowired
    TbClassService tbClassService;
    @Autowired
    TbCounselorService tbCounselorService;
    @Autowired
    TbCollegeService tbCollegeService;
    @RequestMapping("/find")
    public Result find(@RequestBody TbStudent tbStudent){
        QueryWrapper<TbStudent> wrapper=new QueryWrapper<>();
        wrapper.eq(tbStudent.getClassId()!=null,"class_id",tbStudent.getClassId());
        wrapper.eq(tbStudent.getCollegeId()!=null,"college_id",tbStudent.getCollegeId());
        wrapper.eq(tbStudent.getSex()!=null,"sex",tbStudent.getSex());
        List<TbStudent> list=tbStudentService.list();
        for (TbStudent student:list){
            student.setTbCollege(tbCollegeService.getById(student.getCollegeId()));
            student.setTbTeacher(tbTeacherService.getById(student.getTeacher()));
            student.setTbClass(tbClassService.getById(student.getClassId()));
            student.setTbCounselor(tbCounselorService.getById(student.getCounselor()));
        }
        return list!=null?Result.OK(list):Result.ERROR();
    }
    @RequestMapping("/addPoints")
    public Result addPoints(Integer id,Integer newPoints){
        TbStudent student=tbStudentService.getById(id);
        student.setPoints(student.getPoints()+newPoints);
        boolean a=tbStudentService.updateById(student);
        return a?Result.OK():Result.ERROR();
    }
    @RequestMapping("/delPoints")
    public Result delPoints(Integer id,Integer newPoints){
        TbStudent student=tbStudentService.getById(id);
        student.setPoints(student.getPoints()-newPoints);
        boolean a=tbStudentService.updateById(student);
        return a?Result.OK():Result.ERROR();
    }


}
